/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.manage;

import cn.ac.nercis.pes.community.component.controls.PesTextField;
import cn.ac.nercis.pes.community.component.dialog.MessageDialog;
import cn.ac.nercis.pes.community.component.dialog.MessageType;
import cn.ac.nercis.pes.community.mapper.manage.TestedUnitViewMapper;
import cn.ac.nercis.pes.community.model.vo.manage.TestedUnitVO;
import cn.ac.nercis.pes.community.support.FXMLController;
import cn.ac.nercis.pes.community.utils.IconUtils;
import cn.ac.nercis.pes.model.dto.manage.TestedUnitDTO;
import cn.ac.nercis.pes.service.manage.TestedUnitService;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

/**
 * 添加被测信息管理控制器
 *
 * @author lmy
 * @version 1.0.0
 */
@Slf4j
@FXMLController
public class AddTestedUnitController implements Initializable {
    @FXML
    private StackPane page;
    @FXML
    private PesTextField nameField;
    @FXML
    private PesTextField addressField;
    @FXML
    private PesTextField postalCodeField;
    @FXML
    private PesTextField manageField;
    @FXML
    private PesTextField contactPersonField;
    @FXML
    private PesTextField contactPositionField;
    @FXML
    private PesTextField contactDepartmentField;
    @FXML
    private PesTextField contactTelField;
    @FXML
    private PesTextField contactPhoneField;
    @FXML
    private PesTextField contactEmailField;
    private final TestedUnitService testedUnitService;
    private final TestedUnitVO testedUnitVO = new TestedUnitVO();

    @FXML
    private Button saveButton;

    @Autowired
    public AddTestedUnitController(TestedUnitService testedUnitService) {
        this.testedUnitService = testedUnitService;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        saveButton.setGraphic(IconUtils.getGlyph("save-button-icon", "btn-icon"));
    }


    public void initializeData(String testedUnitId) {
        CompletableFuture.supplyAsync(() -> testedUnitService.findById(testedUnitId))
                .thenAccept(res -> Platform.runLater(() -> {
                    //加载被测单位列表
                    initModifyTestedUnitData(res.getData());
                }));
        initTestedUnitControl();
    }

    public void onSave() {
        this.testedUnitVO.setName(StringUtils.trim(this.testedUnitVO.getName()));
        if (!this.nameField.validate()) {
            return;
        }
        TestedUnitDTO unitDTO = TestedUnitViewMapper.INSTANCE.toDTO(this.testedUnitVO);
        CompletableFuture.supplyAsync(() -> testedUnitService.save(unitDTO))
                .thenAccept(res -> Platform.runLater(() -> {
                    if (res.isSuccess()) {
                        MessageDialog.builder()
                                .withType(MessageType.SUCCESS).withMessage("保存成功")
                                .withContainer(this.page)
                                .build().show();
                    } else {
                        MessageDialog.builder()
                                .withType(MessageType.FAIL).withMessage(res.getMsg())
                                .withContainer(this.page)
                                .build().show();
                    }
                }));
    }

    public void initModifyTestedUnitData(TestedUnitDTO unitDTO) {
        testedUnitVO.setId(unitDTO.getId());
        testedUnitVO.setName(unitDTO.getName());
        testedUnitVO.setAddress(unitDTO.getAddress());
        testedUnitVO.setPostalCode(unitDTO.getPostalCode());
        testedUnitVO.setManage(unitDTO.getManage());
        testedUnitVO.setContactPerson(unitDTO.getContactPerson());
        testedUnitVO.setContactPosition(unitDTO.getContactPosition());
        testedUnitVO.setContactDepartment(unitDTO.getContactDepartment());
        testedUnitVO.setContactTel(unitDTO.getContactTel());
        testedUnitVO.setContactPhone(unitDTO.getContactPhone());
        testedUnitVO.setContactEmail(unitDTO.getContactEmail());
    }

    private void initTestedUnitControl() {
        //控件绑定到数据模型
        this.nameField.textProperty().bindBidirectional(testedUnitVO.nameProperty());
        this.addressField.textProperty().bindBidirectional(testedUnitVO.addressProperty());
        this.postalCodeField.textProperty().bindBidirectional(testedUnitVO.postalCodeProperty());
        this.manageField.textProperty().bindBidirectional(testedUnitVO.manageProperty());
        this.contactPersonField.textProperty().bindBidirectional(testedUnitVO.contactPersonProperty());
        this.contactPositionField.textProperty().bindBidirectional(testedUnitVO.contactPositionProperty());
        this.contactDepartmentField.textProperty().bindBidirectional(testedUnitVO.contactDepartmentProperty());
        this.contactTelField.textProperty().bindBidirectional(testedUnitVO.contactTelProperty());
        this.contactPhoneField.textProperty().bindBidirectional(testedUnitVO.contactPhoneProperty());
        this.contactEmailField.textProperty().bindBidirectional(testedUnitVO.contactEmailProperty());
    }
}